home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / netscape / util / Vector.class (.txt) < prev   
Encoding:
Java Class File  |  1997-11-03  |  5.7 KB  |  406 lines

  1. package netscape.util;
  2.  
  3. public class Vector implements Cloneable, Codable {
  4.    Object[] array;
  5.    int count;
  6.    static final String ARRAY_KEY = "array";
  7.  
  8.    public Vector() {
  9.       this.count = 0;
  10.    }
  11.  
  12.    public Vector(int var1) {
  13.       this.array = new Object[var1];
  14.       this.count = 0;
  15.    }
  16.  
  17.    public Object clone() {
  18.       Vector var1;
  19.       try {
  20.          var1 = (Vector)super.clone();
  21.       } catch (CloneNotSupportedException var2) {
  22.          throw new InternalError("Error in clone(). This shouldn't happen.");
  23.       }
  24.  
  25.       if (this.count == 0) {
  26.          var1.array = null;
  27.          return var1;
  28.       } else {
  29.          var1.array = new Object[this.count];
  30.          System.arraycopy(this.array, 0, var1.array, 0, this.count);
  31.          return var1;
  32.       }
  33.    }
  34.  
  35.    public int count() {
  36.       return this.count;
  37.    }
  38.  
  39.    public int size() {
  40.       return this.count;
  41.    }
  42.  
  43.    public boolean isEmpty() {
  44.       return this.count == 0;
  45.    }
  46.  
  47.    public void addElementIfAbsent(Object var1) {
  48.       if (var1 == null) {
  49.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  50.       } else {
  51.          if (var1 != null && !this.contains(var1)) {
  52.             this.addElement(var1);
  53.          }
  54.  
  55.       }
  56.    }
  57.  
  58.    public boolean insertElementBefore(Object var1, Object var2) {
  59.       if (var1 == null) {
  60.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  61.       } else if (var2 == null) {
  62.          return false;
  63.       } else {
  64.          int var3 = this.indexOf(var2);
  65.          if (var3 == -1) {
  66.             return false;
  67.          } else {
  68.             this.insertElementAt(var1, var3);
  69.             return true;
  70.          }
  71.       }
  72.    }
  73.  
  74.    public boolean insertElementAfter(Object var1, Object var2) {
  75.       if (var1 == null) {
  76.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  77.       } else if (var2 == null) {
  78.          return false;
  79.       } else {
  80.          int var3 = this.indexOf(var2);
  81.          if (var3 == -1) {
  82.             return false;
  83.          } else {
  84.             if (var3 >= this.count - 1) {
  85.                this.addElement(var1);
  86.             } else {
  87.                this.insertElementAt(var1, var3 + 1);
  88.             }
  89.  
  90.             return true;
  91.          }
  92.       }
  93.    }
  94.  
  95.    public void addElementsIfAbsent(Vector var1) {
  96.       if (var1 != null) {
  97.          int var3 = var1.count();
  98.  
  99.          for(int var4 = 0; var4 < var3; ++var4) {
  100.             Object var2 = var1.elementAt(var4);
  101.             if (!this.contains(var2)) {
  102.                this.addElement(var2);
  103.             }
  104.          }
  105.  
  106.       }
  107.    }
  108.  
  109.    public void addElements(Vector var1) {
  110.       if (var1 != null) {
  111.          int var2 = var1.count();
  112.          if (this.array == null || this.count + var2 >= this.array.length) {
  113.             this.ensureCapacity(this.count + var2);
  114.          }
  115.  
  116.          for(int var3 = 0; var3 < var2; ++var3) {
  117.             this.addElement(var1.elementAt(var3));
  118.          }
  119.  
  120.       }
  121.    }
  122.  
  123.    public void removeAll(Object var1) {
  124.       int var2 = this.count();
  125.  
  126.       while(var2-- > 0) {
  127.          if (this.elementAt(var2).equals(var1)) {
  128.             this.removeElementAt(var2);
  129.          }
  130.       }
  131.  
  132.    }
  133.  
  134.    public Object removeFirstElement() {
  135.       return this.count == 0 ? null : this.removeElementAt(0);
  136.    }
  137.  
  138.    public Object removeLastElement() {
  139.       return this.count == 0 ? null : this.removeElementAt(this.count - 1);
  140.    }
  141.  
  142.    public Object replaceElementAt(int var1, Object var2) {
  143.       if (var2 == null) {
  144.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  145.       } else if (var1 >= this.count) {
  146.          throw new ArrayIndexOutOfBoundsException(var1 + " >= " + this.count);
  147.       } else if (var1 < 0) {
  148.          throw new ArrayIndexOutOfBoundsException(var1 + " < 0");
  149.       } else {
  150.          Object var3 = this.elementAt(var1);
  151.          this.array[var1] = var2;
  152.          return var3;
  153.       }
  154.    }
  155.  
  156.    public Object[] elementArray() {
  157.       Object[] var1 = new Object[this.count];
  158.       if (this.count > 0) {
  159.          System.arraycopy(this.array, 0, var1, 0, this.count);
  160.       }
  161.  
  162.       return var1;
  163.    }
  164.  
  165.    public void copyInto(Object[] var1) {
  166.       if (this.count > 0) {
  167.          System.arraycopy(this.array, 0, var1, 0, this.count);
  168.       }
  169.  
  170.    }
  171.  
  172.    public void trimToSize() {
  173.       if (this.count == 0) {
  174.          this.array = null;
  175.       } else {
  176.          if (this.count != this.array.length) {
  177.             this.array = this.elementArray();
  178.          }
  179.  
  180.       }
  181.    }
  182.  
  183.    public void ensureCapacity(int var1) {
  184.       if (this.array == null) {
  185.          this.array = new Object[8];
  186.       }
  187.  
  188.       if (var1 >= this.array.length) {
  189.          int var2;
  190.          if (this.array.length < 8) {
  191.             var2 = 8;
  192.          } else {
  193.             var2 = this.array.length;
  194.          }
  195.  
  196.          while(var2 < var1) {
  197.             var2 = 2 * var2;
  198.          }
  199.  
  200.          Object[] var3 = new Object[var2];
  201.          System.arraycopy(this.array, 0, var3, 0, this.count);
  202.          this.array = var3;
  203.       }
  204.    }
  205.  
  206.    public int capacity() {
  207.       return this.array == null ? 0 : this.array.length;
  208.    }
  209.  
  210.    public Enumeration elements() {
  211.       return new VectorEnumerator(this);
  212.    }
  213.  
  214.    public Enumeration elements(int var1) {
  215.       return new VectorEnumerator(this, var1);
  216.    }
  217.  
  218.    public boolean contains(Object var1) {
  219.       return this.indexOf(var1, 0) != -1;
  220.    }
  221.  
  222.    public boolean containsIdentical(Object var1) {
  223.       return this.indexOfIdentical(var1, 0) != -1;
  224.    }
  225.  
  226.    public int indexOf(Object var1) {
  227.       return this.indexOf(var1, 0);
  228.    }
  229.  
  230.    public int indexOf(Object var1, int var2) {
  231.       for(int var3 = var2; var3 < this.count; ++var3) {
  232.          if (this.array[var3].equals(var1)) {
  233.             return var3;
  234.          }
  235.       }
  236.  
  237.       return -1;
  238.    }
  239.  
  240.    public int indexOfIdentical(Object var1, int var2) {
  241.       for(int var3 = var2; var3 < this.count; ++var3) {
  242.          if (this.array[var3] == var1) {
  243.             return var3;
  244.          }
  245.       }
  246.  
  247.       return -1;
  248.    }
  249.  
  250.    public int indexOfIdentical(Object var1) {
  251.       return this.indexOfIdentical(var1, 0);
  252.    }
  253.  
  254.    public int lastIndexOf(Object var1) {
  255.       return this.lastIndexOf(var1, this.count);
  256.    }
  257.  
  258.    public int lastIndexOf(Object var1, int var2) {
  259.       if (var2 > this.count) {
  260.          throw new ArrayIndexOutOfBoundsException(var2 + " > " + this.count);
  261.       } else {
  262.          for(int var3 = var2 - 1; var3 >= 0; --var3) {
  263.             if (this.array[var3].equals(var1)) {
  264.                return var3;
  265.             }
  266.          }
  267.  
  268.          return -1;
  269.       }
  270.    }
  271.  
  272.    public Object elementAt(int var1) {
  273.       if (var1 >= this.count) {
  274.          throw new ArrayIndexOutOfBoundsException(var1 + " >= " + this.count);
  275.       } else {
  276.          return this.array[var1];
  277.       }
  278.    }
  279.  
  280.    public Object firstElement() {
  281.       return this.count == 0 ? null : this.array[0];
  282.    }
  283.  
  284.    public Object lastElement() {
  285.       return this.count == 0 ? null : this.array[this.count - 1];
  286.    }
  287.  
  288.    public void setElementAt(Object var1, int var2) {
  289.       if (var2 >= this.count) {
  290.          throw new ArrayIndexOutOfBoundsException(var2 + " >= " + this.count);
  291.       } else if (var1 == null) {
  292.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  293.       } else {
  294.          this.array[var2] = var1;
  295.       }
  296.    }
  297.  
  298.    public Object removeElementAt(int var1) {
  299.       if (var1 >= this.count) {
  300.          throw new ArrayIndexOutOfBoundsException(var1 + " >= " + this.count);
  301.       } else {
  302.          Object var2 = this.array[var1];
  303.          int var3 = this.count - var1 - 1;
  304.          if (var3 > 0) {
  305.             System.arraycopy(this.array, var1 + 1, this.array, var1, var3);
  306.          }
  307.  
  308.          --this.count;
  309.          this.array[this.count] = null;
  310.          return var2;
  311.       }
  312.    }
  313.  
  314.    public void insertElementAt(Object var1, int var2) {
  315.       if (var2 >= this.count + 1) {
  316.          throw new ArrayIndexOutOfBoundsException(var2 + " >= " + this.count);
  317.       } else if (var1 == null) {
  318.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  319.       } else {
  320.          if (this.array == null || this.count >= this.array.length) {
  321.             this.ensureCapacity(this.count + 1);
  322.          }
  323.  
  324.          System.arraycopy(this.array, var2, this.array, var2 + 1, this.count - var2);
  325.          this.array[var2] = var1;
  326.          ++this.count;
  327.       }
  328.    }
  329.  
  330.    public void addElement(Object var1) {
  331.       if (var1 == null) {
  332.          throw new NullPointerException("It is illegal to store nulls in Vectors.");
  333.       } else {
  334.          if (this.array == null || this.count >= this.array.length) {
  335.             this.ensureCapacity(this.count + 1);
  336.          }
  337.  
  338.          this.array[this.count] = var1;
  339.          ++this.count;
  340.       }
  341.    }
  342.  
  343.    public boolean removeElement(Object var1) {
  344.       int var2 = this.indexOf(var1);
  345.       if (var2 < 0) {
  346.          return false;
  347.       } else {
  348.          this.removeElementAt(var2);
  349.          return true;
  350.       }
  351.    }
  352.  
  353.    public boolean removeElementIdentical(Object var1) {
  354.       int var2 = this.indexOfIdentical(var1, 0);
  355.       if (var2 < 0) {
  356.          return false;
  357.       } else {
  358.          this.removeElementAt(var2);
  359.          return true;
  360.       }
  361.    }
  362.  
  363.    public void removeAllElements() {
  364.       for(int var1 = 0; var1 < this.count; ++var1) {
  365.          this.array[var1] = null;
  366.       }
  367.  
  368.       this.count = 0;
  369.    }
  370.  
  371.    public void sort(boolean var1) {
  372.       Sort.sort(this.array, (Object[])null, 0, this.count, var1);
  373.    }
  374.  
  375.    public void sortStrings(boolean var1, boolean var2) {
  376.       Sort.sortStrings(this.array, 0, this.count, var1, var2);
  377.    }
  378.  
  379.    public String toString() {
  380.       return FormattingSerializer.serializeObject(this);
  381.    }
  382.  
  383.    public void describeClassInfo(ClassInfo var1) {
  384.       var1.addClass("netscape.util.Vector", 1);
  385.       var1.addField("array", (byte)19);
  386.    }
  387.  
  388.    public void encode(Encoder var1) throws CodingException {
  389.       if (this.count != 0) {
  390.          var1.encodeObjectArray("array", this.array, 0, this.count);
  391.       }
  392.    }
  393.  
  394.    public void decode(Decoder var1) throws CodingException {
  395.       this.array = var1.decodeObjectArray("array");
  396.       if (this.array == null) {
  397.          this.count = 0;
  398.       } else {
  399.          this.count = this.array.length;
  400.       }
  401.    }
  402.  
  403.    public void finishDecoding() throws CodingException {
  404.    }
  405. }
  406.